package com.han.neo4jjava.neo4j.location;

import com.han.neo4jjava.core.location.Location;
import com.han.neo4jjava.core.location.LocationRepo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

import static java.util.stream.Collectors.toList;

@Component
@RequiredArgsConstructor
public class Neo4jLocationRepoImpl implements LocationRepo {

    private final locationDao locationDao;

    @Override
    public List<Location> findAll() {
        List<LocationModel> localModels = locationDao.findAll();
        return localModels.stream().map(LocationModel::convert2Location).collect(toList());
    }

    @Override
    public Location findById(Long id) {
        Optional<LocationModel> optional = locationDao.findById(id);
        return optional.map(LocationModel::convert2Location).orElse(null);
    }

    @Override
    public Location save(Location location) {
        LocationModel locationModel = locationDao.save(LocationModel.convert2LocationModel(location));
        location.setId(locationModel.getId());
        return locationModel.convert2Location();
    }

    @Override
    public void saveAll(List<Location> locations) {
        locationDao.saveAll(locations.stream().map(LocationModel::convert2LocationModel).collect(toList()));
    }

}
